<?php


namespace app\index\controller;


use app\index\model\ToolsCount;
use app\index\service\ScriptsService;
use GuzzleHttp\Exception\GuzzleException;
use library\Controller;
use think\facade\Cache;
use think\facade\Request;

class Scripts extends Controller
{
    private $clientIp;

    /**
     * Scripts constructor.
     */
    public function __construct()
    {
        $this->clientIp = Request::ip();
    }

    /**
     * 脚本安装
     * @param ScriptsService $scriptsService
     * @throws GuzzleException
     */
    public function index(ScriptsService $scriptsService)
    {
        $markdown = Cache::remember(SCRIPT_DESC_HTML, function (ScriptsService $scriptsService) {
            return $scriptsService->parseMarkdown(
                Cache::remember(SCRIPT_DESC, function (ScriptsService $scriptsService) {
                    return $scriptsService->getMd();
                })
            );
        });
        $this->assign('markdown', $markdown);
        return $this->fetch();
    }

    /**
     * 展示渲染后的HTML
     * @param ScriptsService $scriptsService
     */
    public function html(ScriptsService $scriptsService)
    {
        $markdown = Cache::remember(SCRIPT_DESC_HTML, function (ScriptsService $scriptsService) {
            return $scriptsService->parseMarkdown(
                Cache::remember(SCRIPT_DESC, function (ScriptsService $scriptsService) {
                    return $scriptsService->getMd();
                })
            );
        });
        echo $markdown;
    }

    /**
     * 脚本内容-安装-计次
     * @param ScriptsService $scriptsService
     * @return string
     * @throws GuzzleException
     */
    public function js(ScriptsService $scriptsService)
    {
        return $this->redirect('https://scriptcat.org/scripts/code/72/%E3%80%90%E7%8E%A9%E7%9A%84%E5%97%A8%E3%80%91VIP%E5%B7%A5%E5%85%B7%E7%AE%B1%2C%E7%99%BE%E5%BA%A6%E6%96%87%E5%BA%93%E8%A7%A3%E6%9E%90%E5%AF%BC%E5%87%BA%2C%E5%85%A8%E7%BD%91VIP%E8%A7%86%E9%A2%91%E5%85%8D%E8%B4%B9%E7%A0%B4%E8%A7%A3%E5%8E%BB%E5%B9%BF%E5%91%8A%2C%E4%B8%80%E7%AB%99%E5%BC%8F%E9%9F%B3%E4%B9%90%E6%90%9C%E7%B4%A2%E4%B8%8B%E8%BD%BD%2C%E8%8E%B7%E5%8F%96B%E7%AB%99%E5%B0%81%E9%9D%A2%2C%E4%B8%8B%E8%BD%BDB%E7%AB%99%E8%A7%86%E9%A2%91%E7%AD%89%E4%BC%97%E5%A4%9A%E5%8A%9F%E8%83%BD%E8%81%9A%E5%90%88%20%E9%95%BF%E6%9C%9F%E6%9B%B4%E6%96%B0%2C%E6%94%BE%E5%BF%83%E4%BD%BF%E7%94%A8.user.js', 301);
        $scriptsService->dataCount(true, Request::ip());
        $js = Cache::remember(SCRIPT_KEY, function () use ($scriptsService) {
            return $scriptsService->getJs();
        });
        $js = $js ? $js : "";
        return response($js, 200, ['Content-Type' => 'application/javascript;charset=utf-8']);
    }

    /**
     * 脚本头信息-计次
     * @param ScriptsService $scriptsService
     * @return string
     * @throws GuzzleException
     */
    public function meta(ScriptsService $scriptsService)
    {
        return $this->redirect('https://scriptcat.org/scripts/code/72/%E3%80%90%E7%8E%A9%E7%9A%84%E5%97%A8%E3%80%91VIP%E5%B7%A5%E5%85%B7%E7%AE%B1%2C%E7%99%BE%E5%BA%A6%E6%96%87%E5%BA%93%E8%A7%A3%E6%9E%90%E5%AF%BC%E5%87%BA%2C%E5%85%A8%E7%BD%91VIP%E8%A7%86%E9%A2%91%E5%85%8D%E8%B4%B9%E7%A0%B4%E8%A7%A3%E5%8E%BB%E5%B9%BF%E5%91%8A%2C%E4%B8%80%E7%AB%99%E5%BC%8F%E9%9F%B3%E4%B9%90%E6%90%9C%E7%B4%A2%E4%B8%8B%E8%BD%BD%2C%E8%8E%B7%E5%8F%96B%E7%AB%99%E5%B0%81%E9%9D%A2%2C%E4%B8%8B%E8%BD%BDB%E7%AB%99%E8%A7%86%E9%A2%91%E7%AD%89%E4%BC%97%E5%A4%9A%E5%8A%9F%E8%83%BD%E8%81%9A%E5%90%88%20%E9%95%BF%E6%9C%9F%E6%9B%B4%E6%96%B0%2C%E6%94%BE%E5%BF%83%E4%BD%BF%E7%94%A8.meta.js', 301);
        $scriptsService->dataCount(false, Request::ip());
        $meta = Cache::get(SCRIPT_META);
        if (!$meta) {
            $jsContent = Cache::remember(SCRIPT_KEY, function () use ($scriptsService) {
                return $scriptsService->getJs();
            });
            $meta = Cache::remember(SCRIPT_META, function () use ($scriptsService, $jsContent) {
                return $scriptsService->getMeta($jsContent);
            });
        }
        $meta = $meta ? $meta : "";
        return response($meta, 200, ['Content-Type' => 'application/javascript;charset=utf-8']);;
    }

    /**
     * 更新脚本信息
     * @param ScriptsService $scriptsService
     * @return string
     * @throws GuzzleException
     */
    public function trigger(ScriptsService $scriptsService)
    {
        $js = $scriptsService->getJs();
        if ($js) {
            Cache::set(SCRIPT_KEY, $js);
        }

        $md = $scriptsService->getMd();
        if ($md) {
            Cache::set(SCRIPT_DESC, $md);
            Cache::rm(SCRIPT_DESC_HTML);
        }

        $meta = $scriptsService->getMeta($js);
        if ($meta) {
            Cache::set(SCRIPT_META, $meta);
        }
        return 'ok';
    }

    public function countTest(ScriptsService $scriptsService)
    {
        $scriptsService->dataCount(true, Request::ip());
        return "ok";
    }
}